<?php/*
require_once('Logger.php');
require_once('config.php');
require_once('dbaccess.php');
require_once('JSON.php');

date_default_timezone_set('Asia/Manila');

$json = new Services_JSON();
$dbh  = new DBAccess($db,$user,$pass);
$logger = new Logger($log_config);
$got_disconected = true;

echo "Started....\n\r";
while (true) {
    try {
        
        if (!$dbh->is_connected()) { 
            echo "Connecting to DTR DB...\n\r";
            $dbh->disconnect();
            $dbh->connect();
            sleep($sleep);
        } elseif (!$dbh->has_internet()) {
            echo "Cannot access SMS Server will retry in 5 seconds...\n\r";
            $got_disconected = true;
            sleep(5);
        }elseif ($date != date("Ymd")) {
            $date = date("Ymd");
            $time = false;
        }else{
            if ($got_disconected){
                echo "Getting last record.. \n\r";
                $config = $json->decode($dbh->get_config());
                $time = $config->time;
                $date = $config->date; 
                $got_disconected = false;
            }
            $res = $dbh->get_student($date, $time);
            foreach ($res as $r):
                $stud_no = (empty($r['idno'])) ? $r['e_id'] : $r['idno'];  
                $datetime = date('m-d-y h:i:sA', strtotime($r['e_date'].$r['e_time']));
                $minfo = $dbh->send_sms($stud_no, $r['e_date'], $r['e_time'], $r['e_mode']);
                $minfo .= ": ID: $stud_no DATE: {$datetime} Sending SMS to {$r['e_name']} : MODE: {$r['e_mode']}\n\r";
                echo $minfo;
                $logger->info($minfo);
                $time = $r['e_time'];
                $date =  $r['e_date'];
            endforeach;
        } 
    } catch (Exception $e) {
        echo "Error: $e";
    }
    sleep($sleep);
}
echo "Exiting..............."
*/
require_once('Logger.php');
require_once('config.php');
require_once('dbaccess.php');
require_once('JSON.php');

date_default_timezone_set('Asia/Manila');

$json = new Services_JSON();
$dbh  = new DBAccess($db,$user,$pass);
$logger = new Logger($log_config);
$got_disconected = true;

echo "Started....\n\r";
while (true) {
    try {
        if (!$dbh->is_connected()) { 
            echo "Connecting to DTR DB...\n\r";
            $dbh->disconnect();
            $dbh->connect();
            sleep($sleep);
        } elseif (!$dbh->has_internet()) {
            echo "Cannot access SMS Server will retry in 5 seconds...\n\r";
            $got_disconected = true;
            sleep(5);
        }else{
            if ($got_disconected){
                
                $config = $json->decode($dbh->get_config());
                $time = $config->time;
                $date = $config->date;
                $n1date = date(Ymd);
              	  if ($n1date!=$date){
	      	     $cdate = date(Ymd);
	              }else{
		           $cdate = $date;
	              }
		    echo "Getting last record.." . "(" . $date . ")" . "..\n\r";
                $got_disconected = false;

            }
            echo "Fetching New Record.... \n\r";  
            $res = $dbh->get_student($date, $time);
            foreach ($res as $r):
                $stud_no = (empty($r['idno'])) ? $r['e_id'] : $r['idno'];  
                $datetime = date('m-d-y h:i:sA', strtotime($r['e_date'].$r['e_time']));
                $minfo = $dbh->send_sms($stud_no, $r['e_date'], $r['e_time'], $r['e_mode']);
                $minfo .= ": ID: $stud_no DATE: {$datetime} Sending SMS to {$r['e_name']} : MODE: {$r['e_mode']}\n\r";
                echo $minfo;
                $logger->info($minfo);
                $time = $r['e_time'];
                $date = $r['e_date'];
            endforeach;
              $ndate = date(Ymd);
              echo "Date Now: ". $ndate . " - " . "Date Config: " . $cdate . " - " . "Time:" . " - " . $time . "..\n\r";
               if($ndate!=$cdate){
                       echo "Date has been Change...\n\r";
	                $config = $json->decode($dbh->get_config());
	                $time = $config->time;
	                $date = $config->date; 
				if ($ndate!=$date){
		      	     $cdate = date(Ymd);
		              }else{
			           $cdate = $date;
	            	  }
      	          echo "Getting last record.." . "(" . $date . ")" . "..\n\r";
                }
        } 
    } catch (Exception $e) {
        echo "Error: $e";
    }
    sleep($sleep);
}
echo "Exiting..............."

?>
